home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / math.swg / 0091_GCD Algorithm in BASM.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-05-26  |  597 b   |  31 lines

  1. {
  2. >> Is there a faster way to find the GCD of two numbers than
  3. >> Euclid's algorithm?
  4.  
  5. > Euclid's algorithm isn't fast enough for you?
  6.  
  7. Oh yes, I gather it is, but I'm interested in whether there are faster
  8. ways or not.
  9.  
  10. > quick div or mod.  The following should be fast enough.
  11. > function GCD( u, v : LongInt) : LongInt;
  12.  
  13. Here's my implementation:
  14. }
  15.  
  16. function gcd(a, b : word) : word; assembler;
  17. asm
  18.   mov     ax,a
  19.   mov     bx,b
  20. @start:
  21.   or      bx,bx
  22.   jz      @endgcd
  23.   xor     dx,dx
  24.   div     bx
  25.   mov     ax,bx
  26.   mov     bx,dx
  27.   jmp     @start
  28. @endgcd:
  29. end;
  30.  
  31.